define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'bootstrap-table-jumpto'], function ($, undefined, Backend, Table, Form,showJumpto) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'zbtaskcompany/zbtask/index' + location.search,
                    add_url: 'zbtaskcompany/zbtask/add',
                    edit_url: 'zbtaskcompany/zbtask/edit',
                    del_url: 'zbtaskcompany/zbtask/del',
                    multi_url: 'zbtaskcompany/zbtask/multi',
                    table: 'zbtask',
                }
            });

            var table = $("#table");
            table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);
                Form.events.selectpage(form);
                var str = '<div class="form-group col-xs-12 col-sm-6 col-md-4 col-lg-3">' +
                    '<label class="control-label col-xs-4"  >省市县</label> ' +
                    '<div class="col-xs-8" data-toggle="cxselect" data-selects="province,city,area"> ' +
                    '<div class="row">' +
                    '<input type="hidden" class="operate" data-name="province" value="=" readonly name="province-province"/>' +
                    '<input type="hidden" class="operate" data-name="city" value="=" readonly name="province-city"/>' +
                    '<input type="hidden" class="operate" data-name="area" value="=" readonly name="province-area"/>' +
                    '<select class="province form-control col-xs-3" name="province" data-url="ajax/area" style="width: 32%">' +
                    '</select> ' +
                    '<select class="city form-control col-xs-3" name="city" data-url="ajax/area" style="width: 32%">' +
                    '</select> ' +
                    '<select class="area form-control col-xs-3" name="area" data-url="ajax/area" style="width: 32%">' +
                    '</select> ' +
                    '</div>' +
                    '</div>' +
                    '</div>';

                $('.row', form).prepend(str)
                Form.events.cxselect(form);

            });
            $.fn.bootstrapTable.locales[Table.defaults.locale].formatSearch = function(){return "ID/任务/行业/公司";};
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                showJumpto: true,
                searchFormVisible: true,
                showExport: false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: 'ID'},
                        {field: 'name', title: __('Name'), operate: 'like'},
                        {field: 'image', title: __('Image'), events: Table.api.events.image, formatter: Table.api.formatter.image,operate: false},
                        {field: 'zbcategory_id', visible: false,title: __('Zbcategory_id'),addClass: "selectpage", extend: "data-source='zbtaskcompany/zbcategory/index' data-field='name' data-order-by='createtime desc'"},
                        {field: 'zbcategory.name', title: __('Zbcategory.name'), operate: false},
                        {field: 'shutdownswitch', title: __('Shutdownswitch'), operate: false,searchList: {"0":__('Shutdownswitch 0'),"1":__('Shutdownswitch 1')}, formatter: Table.api.formatter.toggle},
                        {field: 'status', title: __('Status'),  operate: false,searchList: {"0":__('Status 0'),"1":__('Status 1')}, formatter: Table.api.formatter.toggle},
                        {field: 'type', title: __('Type'), searchList: {"0":__('Type 0'),"1":__('Type 1'),"2":__('Type 2')}, formatter: Table.api.formatter.normal},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'task_money',
                                    text: '众包费',
                                    classname: 'btn btn-xs btn-warning btn-dialog',
                                    icon: 'fa fa-gg-circle',
                                    extend: 'data-area=\'["100%", "100%"]\'',
                                    url: 'zbtaskcompany/money/index?task_id={ids}'
                                },
                                {
                                    name: 'upload',
                                    title: '上传',
                                    text: function (row) {
                                        if (row.is_process === 1) {
                                            return '导入中...';
                                        } else {
                                            return '上传';
                                        }
                                    },
                                    classname: 'btn btn-xs btn-info btn-dialog upload',
                                    icon: function (row) {
                                        if (row.is_process === 1) {
                                            return 'fa fa-spinner';
                                        } else {
                                            return 'fa fa-upload';
                                        }
                                    },
                                    extend: 'data-area=\'["40%", "50%"]\'',
                                    url: 'zbtaskcompany/zbtask/upload?task_id={ids}',
                                    disable: function (row) {
                                        if (row.is_process === 1) {
                                            return true;
                                        } else {
                                            return false;
                                        }
                                    },
                                },
                                {
                                    name: 'files',
                                    text: '材料',
                                    classname: 'btn btn-xs btn-warning btn-dialog',
                                    icon: 'fa fa-folder',
                                    extend: 'data-area= \'["100%", "100%"]\'',
                                    url: 'zbtask_document/index?task_id={ids}'
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'zbtaskcompany/zbtask/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'zbtaskcompany/zbtask/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'zbtaskcompany/zbtask/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        upload: function() {
            console.log('faeffafafeaf')
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});